/* 
    <provides OS-like functionality using a time-triggered scheduler>
    Copyright (C) <2012>  <Jonathan D. Armstrong> <jonathan.armstrong AT mavs.uta.edu>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include <stdio.h>

#include "projectdefs.h"

/**
 * @file   tasks.c
 * @Author Jonathan D. Armstrong (jonathan.armstrong@mavs.uta.edu)
 * @date   December, 2012
 * @brief  Example tasks.
 *
 * Example tasks.
 */
 

/* toggle LED; this process should always be created
   and will run if nothing else is available
   FIXME: could be used if priority is implemented
   as an indication of processor usage
   FIXME: could do memory/hardware sanity checks
   in here or put processor to sleep if no other
   tasks need attention
*/
uint8_t idle(uint8_t arg)
{
    #ifdef TASK_DEBUG
        printf("\r\n");
        printf("idle()\r\n");
    #endif
    
    /* turn LED on or off */
    led1_toggle();
    
    #ifdef TASK_DEBUG
        printf("led1_toggle()\r\n");
    #endif
    
    /* sleep for a short period */
    /* FIXME: needs to be something like 1ms sleep */
    delay_1s();
    
    #ifdef TASK_DEBUG
        printf("idle() finished delaying 1 second.\r\n");
    #endif
    
    return 0;
}


/* toggle another LED once a second */
uint8_t blink_1hz(uint8_t arg)
{
    /* retain variable value between task calls */
    static uint8_t countVar = 0;
    
    #ifdef TASK_DEBUG
        printf("\r\n");
        printf("blink_1hz()\r\n");
    #endif
    
    /* turn LED on or off */
    led2_toggle();
    
    #ifdef TASK_DEBUG
        printf("led2_toggle()\r\n");
    #endif
    
    /* display count variable to demonstrate static definition */
    printf("countVar = %u\r\n", countVar);
    countVar++;
    
    /* set task to delayed state */
    delay_task(&blink_1hz);
    
    return 0;
}